var ctx=window.daijia.ctx;
var freightyear,freightmonths,freightcompanyId;
var freightxdData = new Array(12);
var freightcjData = new Array(12);
var freightjeData = new Array(12);
var freightMonth="";
var freightDay="";
var freightHour="";	
$(function(){
	function freightTableload(){
		
		$.ajax({
			
	        url : ctx+'/chart/freightTableOrderMonthData',
	        type:'GET',
	        async:true,
	        dataType : 'json',
	        data : {
	        
	        	"year":$("#freightYear").val(),
	        	"companyId":$("#freightCompanyId").val()
	        },
	        success : function(result){
	        	var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\月份</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th><th>10</th><th>11</th><th>12</th></tr>";
	        	var tr1='<td>销单量</td>';
	        	var tr2='<td>完成单量</td>';
	        	var tr3='<td>金额(元)</td>';
	        	$("#freightHead tr").remove();
	        	$("#freightHead").append(head);
	        	if(result.length>0){
	        newcontent+="<tbody style='display:none;'>";		
	        		$(result).each(function(index,data){
	        		//"<td><a href='"+ctx+"/chart/tableOrderDay?"+"year="+$("#year").val()+"&month="+data.time+"&companyId="+companyId+"' target='_blank'>"+data.time+"</a></td>"+	
		    tr1+="<td>"+data.cancelAmount+"</td>";
		    tr2+="<td>"+data.finishedAmount+"</td>";
		    tr3+="<td>"+data.money+"</td>"
	        		});
	       newcontent+="</tbody>";		
	        	}
	        	var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	        	$('.floadblacklayer').remove();
	        	$("#freightTable tbody").remove();
	        	$("#freightTable").append(newcontent);
	        	$("#freightTable tbody").show(300);
	    	}
		});
	}
	
	freightTableload();
	
	
	
	$("#freight_export_button").click(function(){
		window.location=ctx+"/chart/freightExport?exportYear="+$("#freightYear").val()+(isMain?"&exportCompanyId="+$("#freightCompanyId").val():"")+
		 "&exportMonth="+freightMonth+"&exportDay="+freightDay;;
	});
	$("#freight_search_button").click(function(){
		freightyear = $("#freightYear").val();
		freightcompanyId = $("#freightCompanyId").val();
		var errandMonth="";
		var errandDay="";
		var errandHour="";
		$("#freight").css("width","100%");
		var y = $("#freightYear").val();
		var c=$("#freightCompanyId").val();
		var cname =$('#freigtCompanyId').find("option:selected").text();
		if($('.floadblacklayer').length==0){
			var dw = $("#freightTab").width();
			var dh = $("#freightTab").height();
			var backlayer ="<div class='floadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
			$("#freightTab").append(backlayer);
			$('.floadblacklayer').css({"z-index":"12","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
		}
		
		
		
		$.post(window.daijia.ctx+'/chart/freightDataForYear',{'year':y,'companyId':c},function(data){
			$('.floadblacklayer').remove();
			freightmonths = data.months;
			
				for(var i=0 ; i<freightmonths.length ; i++){
					var month =freightmonths[i];
					freightxdData[month.month] = month.totalCancelNum;
					freightcjData[month.month] = month.totalSuccessNum;
					freightjeData[month.month] = month.totalPrice;
				}
				freightTableload();
				freightRenderYearChart(freightxdData,freightcjData,freightjeData,freightmonths);
			
		},'json');
		
	});
	
	
	Highcharts.setOptions({
		lang:{
			printChart:'打印报表',
			downloadJPEG:'保存为JPG图片',
			downloadPDF: '保存为PDF文档',
			downloadPNG: '保存为PNG图片',
			downloadSVG: '保存为SVG文档',
			loading:'加载中...',
			resetZoom:'重置缩放',
			resetZoomTitle:'缩放比例：1:1',
			months:['1月', '2月', '3月', '4月', '5月', '6月','7月', '8月', '9月', '10月', '11月', '12月']
		}
	});
	
	freightyear = $("#freightYear").val();
	freightcompanyId = $("#freightCompanyId").val();
	
	for(var i=0 ; i<12 ; i++){
		freightxdData[i] = 0;
		freightcjData[i] = 0;
		freightjeData[i] = 0;
	}
	
	
	$.post(window.daijia.ctx+'/chart/freightDataForYear',{'year':freightyear,'companyId':freightcompanyId},function(data){
		
		freightmonths = data.months;
		
		if(freightmonths){
			for(var i=0 ; i<freightmonths.length ; i++){
				
				
				var month = freightmonths[i];
				freightxdData[month.month] = month.totalCancelNum;
				freightcjData[month.month] = month.totalSuccessNum;
				freightjeData[month.month] = month.totalPrice;
			}
			
			freightRenderYearChart(freightxdData,freightcjData,freightjeData,freightmonths);
		}else{
			$.scojs_message('没有订单记录', $.scojs_message.TYPE_OK);
		}
		
	},'json');
	
	/*$("#year").change(function(){
		$("#submit_form").submit();
	});
	
	$("#companyId").change(function(){
		$("#submit_form").submit();
	});*/
	
});

freightRenderYearChart=function(xdData,cjData,jeData,months){
	$("#freight").css("width",$(".container-fluid .navigation").width());
	$("#freight").highcharts({
		chart: {
            renderTo: 'freight',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		$("#freight").css("width","100%");
	            		var category = e.point.category;
	            		var month = parseInt(category.substring(0,category.length-1))-1;
	            		freightMonth=month;
	            		$.post(window.daijia.ctx+'/chart/freightDataForMonth',{'year':$("#freightYear").val(),'month':month,"companyId":$("#freightCompanyId").val()},function(data){
	            			var dataHours = data.hours;
	            			var hours = new Array();
	            			var days=data.chartDays;
	            			var xdmData = new Array();
	            			var cjmData = new Array();
	            			var jemData = new Array();
	            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\天</th>";
	            			var tr1='<td>销单量</td>';
	            			var tr2='<td>完成单量</td>';
	            			var tr3='<td>金额(元)</td>';
	            			for(var i = 0 ; i < days.length ; i++){
	            				var chartDay = days[i];
	            				days[i] = chartDay.day;
	            				xdmData[i] = chartDay.totalCancelNum;
	            				cjmData[i] = chartDay.totalSuccessNum;
	            				jemData[i] = chartDay.totalPrice;
	            				head+="<th>"+chartDay.day+"</th>";
	            				tr1+="<td>"+chartDay.totalCancelNum+"</td>";
	            				tr2+="<td>"+chartDay.totalSuccessNum+"</td>";
	            				tr3+="<td>"+chartDay.totalPrice+"</td>";
	            			}
	            			head=head+"</tr>"
	            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	            			$("#freightHead tr").remove();
	            			$("#freightHead").append(head);
	            			$("#freightTable tbody").remove();
	            			$("#freightTable").append(newcontent);
	            			$("#freithtTable tbody").show(300);
		            		
	            			freightRenderMonthChart(freightyear,month,days,xdmData,cjmData,jemData);
	            			
	            		},'json');
/*	            		var category = e.point.category;
	            		//加载月数据 
	            		var year = $("#year").val();
	            		var month = parseInt(category.substring(0,category.length-1))-1;
	            		var days = new Array();
	            		var xdmData = new Array();
	            		var cjmData = new Array();
	            		var jemData = new Array();
	            		
            			var currentMonth = months[month];
            			var days = currentMonth.chartDays;
            			for(var i=0;i<mouths.length;i++){
            				
            				xdmData[i]=mouths[i].totalCancelNum;
            				cjData[i] = mouths[i].totalSuccessNum;
            				jemData[i]=mouths[i].totalPrice;
            				
            			}
            			
            			
            			for(var i = 0 ; i < days.length ; i++){
            				var chartDay = days[i];
            				days[i] = chartDay.day;
            				xdmData[i] = chartDay.totalCancelNum;
            				cjmData[i] = chartDay.totalSuccessNum;
            				jemData[i] = chartDay.totalPrice;
            			}
            			
	            		
            			errandRenderMonthChart(year,month,days,xdmData,cjmData,jemData);*/
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '专车订单统计图'
        },
        subtitle: {
            text: freightyear+'年'
        },
        xAxis: [{
            categories: Highcharts.getOptions().lang.months
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};


freightRenderMonthChart=function(year,month,days,xdData,cjData,jeData){
	
	$("#freight").highcharts({
		chart: {
            renderTo:  'freight',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		var category = e.point.category;
	            		//加载某天数据 
	            		
	            		freightDay=category;
	            		$.post(window.daijia.ctx+'/chart/freightDataForDay',{'year':year,'month':month,'day':category,"companyId":$("#freightCompanyId").val()},function(data){
	            			
	            			var dataHours = data.hours;
	            			var hours = new Array();
	            			var xddData = new Array();
	            			var cjdData = new Array();
	            			var jedData = new Array();
	            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\小时</th>";
	            			var tr1='<td>销单量</td>';
	            			var tr2='<td>完成单量</td>';
	            			var tr3='<td>金额(元)</td>';
	            			for(var i=0 ; i<dataHours.length ; i++){
	            				var hour = dataHours[i];
	            				hours[i] = hour.hour;
	            				xddData[i] = hour.totalCancel;
	            				cjdData[i] = hour.totalSuccess;
	            				jedData[i] = hour.totalPrice;
	            				head+="<th>"+hour.hour+"</th>";
	            				tr1+="<td>"+hour.totalCancel+"</td>";
	            				tr2+="<td>"+hour.totalSuccess+"</td>";
	            				tr3+="<td>"+hour.totalPrice+"</td>";
	            			}
	            			head=head+"</tr>"
	            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	            			$("#freightHead tr").remove();
	            			$("#freightHead").append(head);
	            			$("#freightTable tbody").remove();
	            			$("#freightTable").append(newcontent);
	            			$("#freightTable tbody").show(300);
	            			
	            			freightRenderDayChart(year,month,category,hours,xddData,cjdData,jedData);
	            			
	            		},'json');
	            		
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '专车订单统计图'
        },
        subtitle: {
            text: freightyear+"年-"+(month+1)+"月"
        },
        xAxis: [{
            categories: days
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};

freightRenderDayChart=function(year,month,day,hours,xddData,cjdData,jedData){
	
	$("#freight").highcharts({
		chart: {
            renderTo: 'freight',
            reflow:true,
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		//TODO 返回年数据统计
	            		freightDay="";
	            		freightMonth="";
	            		freightHour="";
	            		var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\月份</th>";
            			var tr1='<td>销单量</td>';
            			var tr2='<td>完成单量</td>';
            			var tr3='<td>金额(元)</td>';
	            		for(var i=0;i<freightmonths.length;i++){
	            			head+="<th>"+((freightmonths[i].month)+1)+"</th>";
	            			tr1+="<td>"+(freightmonths[i]).totalCancelNum+"</td>";
            			    tr2+="<td>"+(freightmonths[i]).totalSuccessNum+"</td>";
            			    tr3+="<td>"+(freightmonths[i]).totalPrice+"</td>";
	            		}
	            		head=head+"</tr>"
            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
            			$("#freightHead tr").remove();
            			$("#freightHead").append(head);
            			$("#freightTable tbody").remove();
            			$("#freightTable").append(newcontent);
            			$("#freightTable tbody").show(300);
	            		freightRenderYearChart(freightxdData,freightcjData,freightjeData,freightmonths);
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '专车订单统计图'
        },
        subtitle: {
            text: freightyear+"年-"+(month+1)+"月-"+day+"日"
        },
        xAxis: [{
            categories: hours
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jedData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjdData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xddData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};